package com.modudf.common;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import com.modudf.exception.ConnectiontException;

public class JdbcUtil {

	private static String db_url;
	private static String db_username;
	private static String db_password;

	public static Connection getConnection(String url, String userName,
			String password) throws ConnectiontException {
		Connection conn = null;
		db_url = url;
		db_username = userName;
		db_password = password;
		try {
			Class.forName(Constants.DB_DRIVER);
			conn = DriverManager
					.getConnection(db_url, db_username, db_password);
		} catch (ClassNotFoundException e) {
			throw new ConnectiontException(e.getMessage());
		} catch (SQLException e) {
			throw new ConnectiontException(e.getMessage());
		}
		return conn;
	}

	public static Connection getConnection() {
		try {
			return getConnection(db_url, db_username, db_password);
		} catch (ConnectiontException e) {
			e.printStackTrace();
			return null;
		}
	}

	public static void closeConnection(Connection conn) {
		try {
			conn.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public static void rollbackConnection(Connection conn) {
		try {
			conn.rollback();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public static void commitConnection(Connection conn) {
		try {
			conn.commit();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
